package nbn.android.myyoutubeclient;

import java.util.ArrayList;

import nbn.android.myyoutubeclient.adapter.VideoAdapter;
import nbn.android.myyoutubeclient.object.PlaylistObject;
import nbn.android.myyoutubeclient.object.VideoObject;
import nbn.android.myyoutubeclient.parse.Parser;
import nbn.android.myyoutubeclient.parse.ParserVideo;
import nbn.android.myyoutubeclient.util.CMD;
import nbn.android.myyoutubeclient.util.TagName;
import nbn.android.myyoutubeclient.view.BounceProgressBar;
import nbn.android.myyoutubeclient.view.CustomActionbar;

import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class PlaylistVideoActivity extends ActionBarActivity implements OnScrollListener {
	String tag = PlaylistVideoActivity.class.getName();

	PlaylistObject playlistObject;
	int maxVideo = 10;
	int m_PreviousTotalCount = 0;
	ArrayList<VideoObject> playlistObjects;
	String url;
	ListView listView;
	VideoAdapter adapter;
//	ProgressBar progressBar;
	BounceProgressBar progressBarFooter;
	public CustomActionbar myActionBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlistdetail_layout);
		myActionBar = new CustomActionbar(PlaylistVideoActivity.this, null);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayShowTitleEnabled(false);
//		actionBar.setIcon(android.R.drawable.ic_popup_sync);
		actionBar.setCustomView(myActionBar);
		
		
		listView = (ListView) findViewById(R.id.listViewVideo);
//		progressBar = (ProgressBar) findViewById(R.id.progressBarVideo_item);
		progressBarFooter = (BounceProgressBar) findViewById(R.id.progressBarloadmore);
//		listView.setEmptyView(progressBar);
		playlistObjects = new ArrayList<VideoObject>();
		adapter = new VideoAdapter(PlaylistVideoActivity.this, playlistObjects);
		listView.setAdapter(adapter);
		listView.setOnScrollListener(this);
		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			playlistObject = (PlaylistObject) extras.getSerializable(TagName.KEY);
		if (playlistObject != null && !playlistObject.equals("")) {
			new LoadMoviesAsync().execute("");
			myActionBar.setTitle(playlistObject.gettitle());
		}

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String s = playlistObjects.get(position).getDuration();
				if (s != null && !s.equals("")){
					 playlistObjects.get(position).showinfo();
					setVideoclick(PlaylistVideoActivity.this, playlistObjects.get(position));
				}
				else
					Toast.makeText(PlaylistVideoActivity.this, playlistObjects.get(position).gettitle(), Toast.LENGTH_SHORT).show();
			}
		});
	}

//	public void setVideoclick(final Context context, final int index, final ArrayList<VideoObject> listItem) {
//		Intent intent = new Intent(context, VideoPlayActivity.class);
//		intent.putExtra(TagName.INDEX, index);
//		intent.putExtra(TagName.KEY, listItem);
//		context.startActivity(intent);
//	}

	public void setVideoclick(final Context context, final VideoObject item) {
		Intent intent = new Intent(context, VideoPlayActivity.class);
		intent.putExtra(TagName.KEY, item);
		context.startActivity(intent);
	}

	class LoadMoviesAsync extends AsyncTask<String, VideoObject, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			if (m_PreviousTotalCount>0)
				PlaylistVideoActivity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						progressBarFooter.setVisibility(View.VISIBLE);
					}
				});
			url = String.format(CMD.GDATA_PLAYLIST_VIDEOS, playlistObject.getid(), "" + (m_PreviousTotalCount + 1), "" + maxVideo);
			Log.e(tag, "url: " + url);
			Parser parser = new Parser();
			NodeList movieContentLst = parser.getResponceNodeList(url);
			if (movieContentLst != null) {
				for (int i = 0; i < movieContentLst.getLength(); i++) {
					publishProgress(ParserVideo.getResult(movieContentLst, i));
				}
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(VideoObject... values) {
			super.onProgressUpdate(values);
			addItem(values);
			adapter.notifyDataSetChanged();
		}

		public void addItem(VideoObject... items) {
			for (VideoObject item : items) {
				playlistObjects.add(item);
			}
		}
		@Override
		protected void onPostExecute(Void result) {
			PlaylistVideoActivity.this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					progressBarFooter.setVisibility(View.GONE);
				}
			});
			super.onPostExecute(result);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (totalItemCount == 0 || adapter == null)
			return;
		if (m_PreviousTotalCount == totalItemCount){
			return;
		}
		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount-3;
		if (loadMore && firstVisibleItem > 0 && totalItemCount - m_PreviousTotalCount>=10) {
			m_PreviousTotalCount = totalItemCount;
			new LoadMoviesAsync().execute("");
		}
			
	}
}
